package org.NooLab.glue.msgframe;


import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.xml.XMessageAbs;

import com.jamesmurty.utils.XMLBuilder;



public class BXMessage extends XMessageAbs{

	
	public BXMessage( PrintLog outprn ){
		super();
			
		out = outprn;
	}

	
	public String createIboardPing( BasicMessageParams basics, int mutualRelay ){
		

		String xmlstr = "" ;
		XMLBuilder builder ;

		
		try {
 
			
			builder = getXmlBuilder( "messageboard" ).a( "name", "spela").a("role", basics.roleOfSender ); 
			
			builder.e("connect").a("action", "ping")
					  .e("instance").a("name", basics.instanceName).up()
					  .e("parameters").a("mutualrelay", ""+mutualRelay) 
					  .t("$checkaddress$")
				   .up() ;
			
			  
			
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		return xmlstr;
	}

	
	public String createPingResponse( BasicMessageParams basics, int numberOfReceptors, String targetedAddr ){
		
		String xmlstr = "" ;
		XMLBuilder builder ;

		
		try {
 
			
			builder = getXmlBuilder( "messageboard" ).a( "name", "spela").a("role", basics.roleOfSender ); 
			
			builder.e("connect") 
			         .e("instance").a("name", basics.instanceName).up()
			         .e("origin").a("value",basics.privateTransactionIdentifier).up()
					 .e("environment").a("capacity", ""+numberOfReceptors).up()
					 .e("responsiveaddress").a("value", targetedAddr).up()
			  	   .up()
			;
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		return xmlstr;
	}
	
	
	public String createRelayRequestResponse( BasicMessageParams basics, long transactionID , String requestedDocType, int availability ){
		
		String xmlstr = "" ;
		XMLBuilder builder ;

		
		try {
 
			
			builder = getXmlBuilder( "messageboard" ).a( "name", "spela").a("role", basics.roleOfSender ); 
			
			builder.e("transaction")
			        .e("transactionid").a("value", ""+transactionID).up()
					.e("instance").a("name", basics.instanceName).up()  // mb1-embeddedsrc-iboard-1-bedcd4b1-80a7-4c70-8dc3-a3fe3816b56f
					.e("confirmation").a("type", "relay")
									  .a("transactionid", ""+basics.transactionID)
									  .a("availability", ""+availability)
								 	  .a("secret", basics.privateTransactionIdentifier) // 79cb0377-e9f0-4382-a724-7f7b031e82bb
					
					.up() ;
			
			  
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		return xmlstr;
	}
	
	public String createRelayDoctypeRequest(  BasicMessageParams basics, String doctype, String contextSegment ){
		
		String xmlstr = "" ;
		XMLBuilder builder ;

		
		try {
 
			
			builder = getXmlBuilder( "messageboard" ).a( "name", "spela").a("role", basics.roleOfSender ); 
			
			builder.e("transaction")
					.e("instance").a("name", basics.instanceName).up()  // mb1-embeddedsrc-iboard-1-bedcd4b1-80a7-4c70-8dc3-a3fe3816b56f
					.e("origin").a("id", ""+basics.transactionID).a("type", "native").up()
					.e("request").a("type", "relay")     
								 .a("doctype", doctype)  // "*,doc"
								 .a("secret", basics.privateTransactionIdentifier) // 79cb0377-e9f0-4382-a724-7f7b031e82bb
								 .up()
					.e("relay")
					   .e("address").a("value", basics.hostaddress).up()
					   .e("time").a("value", ""+System.currentTimeMillis()).up()
					   .up()
					   
					.up() ;
			
			  		if (contextSegment.length()>0){
			  		}
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		return xmlstr;
	}
	
	public String template( BasicMessageParams basics,  String action){ 
	 
		String xmlstr = "" ;
		XMLBuilder builder ;

		
		try {
 
			
			builder = getXmlBuilder( "messageboard" ).a( "name", "spela").a("role", basics.roleOfSender ); 
			
			builder.e("subscription")
			
					.up() ;
			
			  
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		return xmlstr;
	
	}
	
	
	
	
	
	
}
